<?php
/**
 * Created by PhpStorm.
 * User: lcw-home
 * Date: 2015/11/14
 * Time: 15:27
 */
/**
 * 检测用户是否登录
 * @return integer 0-未登录，大于0-当前登录用户ID
 */
function is_login(){
    $user = getUserAuth();
    if (null==$user) {
        return false;
    } else {
        return session('user_auth_sign') == data_auth_sign($user);
    }
}
/**
 * 数据签名认证
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function data_auth_sign($data) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}
/**
 * 检测当前用户是否为管理员
 * @return boolean true-管理员，false-非管理员
 */
function is_administrator($uid = null){
    $uid = is_null($uid) ? is_login() : $uid;
    return $uid && (in_array(intval($uid),C('USER_ADMINISTRATOR')));
}

function getAuthGroup($gid=0){
    static $authGroup;
    if(isset($authGroup[$gid]))
        return $authGroup[$gid];
    else{
        $authGroup=getCache('AuthGroup');
    }
    return $authGroup[$gid];
}
function getCache($name,$method=null,$type='model'){
    $cacheName=$type.'_'.$name;
    if(null!=$method)
        $cacheName.='_'.$method;
    $cache=S($cacheName);
    if(false===$cache){
        $model=D($name);
        $methodName=null==$method?'getCache':$method;
        if(method_exists($model,$methodName))
            $cache=call_user_func(array($model,$methodName));
        else
            $cache=array();
        S($cacheName,$cache);
    }
    return $cache;
}
function getUserAuth($name=null){
    static $userAuthInfo;
    if(!isset($userAuthInfo['uid'])){
        if(session('?user_auth'))
            $userAuthInfo=session('user_auth');
        else
            return null;
    }
    if(null==$name)
        return $userAuthInfo;
    else{
        if(isset($userAuthInfo[$name]))
            return $userAuthInfo[$name];
    }
    return null;
}

function superHash($str){
    $h1=md5($str);
    $h2=sha1($str);
    return implode('',array_merge(str_split($h1,3),str_split($h2,3)));
}

/**
 * 检查字符串是否是UTF8编码
 * @param string $string 字符串
 * @return Boolean
 */
function isUtf8($str)
{
    $c    = 0;
    $b    = 0;
    $bits = 0;
    $len  = strlen($str);
    for ($i = 0; $i < $len; $i++) {
        $c = ord($str[$i]);
        if ($c > 128) {
            if (($c >= 254)) {
                return false;
            } elseif ($c >= 252) {
                $bits = 6;
            } elseif ($c >= 248) {
                $bits = 5;
            } elseif ($c >= 240) {
                $bits = 4;
            } elseif ($c >= 224) {
                $bits = 3;
            } elseif ($c >= 192) {
                $bits = 2;
            } else {
                return false;
            }

            if (($i + $bits) > $len) {
                return false;
            }

            while ($bits > 1) {
                $i++;
                $b = ord($str[$i]);
                if ($b < 128 || $b > 191) {
                    return false;
                }

                $bits--;
            }
        }
    }
    return true;
}
